package com.duowei.kitchenshow.print;

import android.text.TextUtils;
import android.util.Log;

import com.duowei.kitchenshow.bean.Cfpb;
import com.duowei.kitchenshow.bean.Cfpb_item;
import com.duowei.kitchenshow.utils.ToastUtil;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2018-01-09.
 */

public class PrintNet {
    private Socket client;
    public boolean IFOpen = false;
    private static PrinterCMD pcmd;
    private PrintWriter mPw;

    private PrintNet() {
    }

    private static PrintNet singleton = null;

    public static PrintNet getInstance() {
        if (singleton == null) {
            singleton = new PrintNet();
            pcmd=PrinterCMD.getPritcmd();
        }
        return singleton;
    }

//    public void printSingle(final String ip, final Cfpb cfpb){
//        new Thread(){
//            @Override
//            public void run() {
//                if (initSocket(ip)) return;
//                try {
//                    mPw = new PrintWriter((new OutputStreamWriter(client.getOutputStream(), "GBK")), true);
//                    mPw.print(pcmd.CMD_SetPos());
//                    mPw.print(pcmd.CMD_TextAlign(0));
//                    mPw.print(pcmd.CMD_TextBold(0));
//                    mPw.print(pcmd.CMD_FontSize(3));
//                    mPw.print(String.format(Locale.CANADA,"餐桌：%s",cfpb.getCzmc()));
//                    mPw.print(pcmd.CMD_FontSize(1));
//                    mPw.println(String.format(Locale.CANADA,"  时间：%s",cfpb.getXdsj().substring(5,16)));
//                    mPw.println(String.format(Locale.CANADA,"点单员：%s",cfpb.getYhmc()));
//                    mPw.print(pcmd.CMD_FontSize(3));
//                    mPw.println("------------------------");
//                    float sl=cfpb.getSl()<=cfpb.getYwcsl()?cfpb.getSl():cfpb.getYwcsl();
//                    mPw.println(String.format("%s%s\t%s", sl,cfpb.getDw(),cfpb.getXmmc()));
//                    if(!TextUtils.isEmpty(cfpb.getPz())){
//                        mPw.println(String.format("要求：%s",cfpb.getPz()));
//                    }
//                    mPw.println("\n\n\n\n");
//                    mPw.print(pcmd.CMD_CutPage());
//                    mPw.close();
//                    client.close();
//                    client=null;
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
//    }

    public void startPrint(final String ip, final List<Cfpb_item> cfpbList){
        new Thread(){
            @Override
            public void run() {
                if (initSocket(ip)) return;
                //打印输出   控制输出字符集位GBK
                try {
                    mPw = new PrintWriter((new OutputStreamWriter(client.getOutputStream(), "GBK")), true);
                    mPw.print(pcmd.CMD_SetPos());
                    mPw.print(pcmd.CMD_TextAlign(0));
                    mPw.print(pcmd.CMD_TextBold(0));
                    mPw.print(pcmd.CMD_FontSize(3));
                    for(int i=0;i<cfpbList.size();i++){
                        Cfpb_item cfpbItem = cfpbList.get(i);
                        mPw.print(String.format(Locale.CANADA,"餐桌：%s",cfpbItem.czmc1));
                        mPw.print(pcmd.CMD_FontSize(1));
                        mPw.println(String.format(Locale.CANADA,"  时间：%s",cfpbItem.xdsj.substring(5,16)));
                        mPw.println(String.format(Locale.CANADA,"点单员：%s",cfpbItem.yhmc));
                        mPw.print(pcmd.CMD_FontSize(3));
                        mPw.println("------------------------");
//                        float sl=cfpb.getSl()<=cfpb.getYwcsl()?cfpb.getSl():cfpb.getYwcsl();
                        float sl=cfpbItem.sl1;
                        mPw.println(String.format("%s%s\t%s", sl,cfpbItem.dw,cfpbItem.xmmc1));
                        if(!TextUtils.isEmpty(cfpbItem.pz)){
                            mPw.println(String.format("要求：%s",cfpbItem.pz));
                        }
                        mPw.println("\n\n\n\n");
                        mPw.print(pcmd.CMD_CutPage());
                    }
                    mPw.close();
                    client.close();
                    client=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


    public boolean initSocket(String ip) {
       if(TextUtils.isEmpty(ip)){
           ToastUtil.showToast("请设置打印机IP");
           return true;
       }
        try {
            if(client!=null){
                client.close();
                client=null;
            }
            client = new Socket();
            InetSocketAddress isa = new InetSocketAddress(ip,9100);//打印设备端口号
            client.connect(isa, 2000);//设置网络超时时间
            IFOpen = true;
        } catch (IOException e) {
            e.printStackTrace();
            IFOpen = false;
        }
        if (!IFOpen) {//这么加判断的原因是因为有的打印机有记忆功能，打印机因为没有打开，
            // 导致异常，再次打开时会接着打印，关闭这种功能
            ToastUtil.showToast("打印失败，请检查网络及打印机IP设置");
            return true;
        }
        return false;
    }
}
